<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class QuotationDetail extends BaseController
{
    protected $title='报价明细';
    protected $table='quotation_order_detail';

    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['qod.status','>',-1];
            if (isset($param['orderNum'])&&$param['orderNum']){
                $where[]=['qo.orderNum','like','%'.$param['orderNum'].'%'];
            }
            if (isset($param['pName'])&&$param['pName']){
                $where[]=['py.name','like','%'.$param['pName'].'%'];
            }
            if (isset($param['saleUnit'])&&$param['saleUnit']){
                $where[]=['d.title','like','%'.$param['saleUnit'].'%'];
            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['md.brand','like','%'.$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like','%'.$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['md.specs','like','%'.$param['specs'].'%'];
            }
           if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['md.buyCode','like','%'.$param['buyCode'].'%'];
            }
           if (isset($param['payType'])&&$param['payType']){
                $where[]=['qopt.id','=',$param['payType']];
            }
           if (isset($param['orderDate'])&&$param['orderDate']){
                $dates=explode('~',$param['orderDate']);
                $where[]=['qo.orderDate','between',$dates];
            }
            $sql=Db::name($this->table)->alias('qod')
                ->leftJoin('quotation_order qo','qo.id=qod.orderId')
                ->leftJoin('material_dossier md','md.id=qod.mdId')
                ->leftJoin('purveyor py','py.id=qo.punterId')
                ->leftJoin('quo_order_pay_type qopt','qopt.id=qo.payType')
                ->leftJoin('department d','d.id=qo.salesUnitId')
                ->where($where);
            if (isset($param['saleUser'])&&$param['saleUser']){
                $uId=Db::name('admin')->where('status',1)
                    ->where('name','=',$param['saleUser'])->value('id');
                $sql->where(function ($query) use ($uId){
                    $query->where('qo.saleId','=' ,$uId);
                    $query->whereOr('qo.saleId','like',$uId.',%');
                    $query->whereOr('qo.saleId','like','%,'.$uId.',%');
                    $query->whereOr('qo.saleId','like','%,'.$uId);
                    return $query;
                });
            }
                $sql->field('qod.*,qo.orderNum,qopt.name as payTypeName,qo.dayLongs,d.title as saleUnit,py.name as pName,
                qo.saleId,qo.tax,qo.orderDate,md.name as mdName,md.brand as mdBrand, md.unit as mdUnit,md.specs,
                md.buyCode')->order('qod.id DESC');

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
                $list=$sql->paginate($rows)->each(function ($item){
                    $saleUsers=Db::name('admin')->where('id','in',$item['saleId'])->column('name');
                    $item['saleName']=implode(',',$saleUsers);
                    return $item;
                });;
            return table_assign(0, '', $list);
        } else {
            $payTypeList=Db::name('quo_order_pay_type')->where('status',1)
                ->field('id,name')->select();
            View::assign('payTypeList',$payTypeList);
            return view();
        }
    }
}